package com.zachary.es.util;

import com.alibaba.fastjson.JSON;
import com.zachary.es.dtd.Medicine;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;

import java.util.ArrayList;
import java.util.List;

/**
 * elasticSearch 结果帮助类
 * <p/>
 * <pre>
 * Created with IntelliJ IDEA.
 * User: zachary.
 * Date: 2014/7/15
 * Time: 10:55
 * PC：windows'IDEA in company <br>
 * </pre>
 *
 * @author zachary.
 */
public class ConvertUtil {

    /**
     * 将hits转换为beans
     *
     * @param hits
     * @return
     */
    public static List<Medicine> hitsToBeans(SearchHit[] hits) {
        List<Medicine> results = new ArrayList<Medicine>();

        for (SearchHit hit : hits) {
            results.add(JSON.parseObject(hit.getSourceAsString(), Medicine.class));
        }
        return results;
    }

    /**
     * 从respone里面拿出hits转换为beans
     *
     * @param response
     * @return
     */
    public static List<Medicine> getBeans(SearchResponse response) {
        return hitsToBeans(response.getHits().hits());
    }

}
